package com.sdy.healthcode.web.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.utils.DesensitizeUtil;
import com.sdy.healthcode.biz.dto.BayonetManageDTO;
import com.sdy.healthcode.biz.dto.CarCheckInInfoDTO;
import com.sdy.healthcode.biz.model.BayonetManage;
import com.sdy.healthcode.biz.model.BuildingManage;
import com.sdy.healthcode.biz.service.VehicleRegistrationService;
import com.sdy.common.utils.DateUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.model.VehicleRegistration;
import com.sdy.common.model.Response;
import com.sdy.healthcode.biz.utils.CopyUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdy.mvc.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cgj
 * @since 2020-03-16
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@Api(tags = "车辆进出登记")
@RequestMapping("/vehicleRegistration")
public class VehicleRegistrationController extends BaseController {
    @Autowired
    private VehicleRegistrationService vehicleRegistrationService;

    @ApiParam(value = "获取车辆进出登记信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "action", value = "1-进/2-出"),
            @ApiImplicitParam(name = "nameOrPhone", value = "姓名或手机号"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "visitDepartment", value = "拜访科室")
    })
    @GetMapping("/getCarCheckInInfo")
    public Response getCarCheckInInfo(Integer action, String nameOrPhone, String startTime, String endTime, String visitDepartment, Page page) throws UnsupportedEncodingException {
        List<CarCheckInInfoDTO> getCarCheckInInfo;
        if (StringUtil.isNotBlank(startTime)) {
            startTime = startTime + " 00:00:00";
        }
        if (StringUtil.isNotBlank(endTime)) {
            endTime = endTime + " 23:59:59";
        }
        if (StringUtil.isNotBlank(nameOrPhone)){
            nameOrPhone = URLDecoder.decode(nameOrPhone, StandardCharsets.UTF_8.name());
        }
        if (StringUtil.isNotBlank(visitDepartment)){
            visitDepartment=URLDecoder.decode(visitDepartment,StandardCharsets.UTF_8.name());
        }
        if (StringUtil.isNotBlank(nameOrPhone) && StringUtils.isNumeric(nameOrPhone)) {
            getCarCheckInInfo = vehicleRegistrationService.getCarCheckInInfo(action, null, nameOrPhone, startTime, endTime, visitDepartment, page);
        } else if (StringUtil.isNotBlank(nameOrPhone) && !StringUtils.isNumeric(nameOrPhone)) {
            getCarCheckInInfo = vehicleRegistrationService.getCarCheckInInfo(action, nameOrPhone, null, startTime, endTime, visitDepartment, page);
        } else {
            getCarCheckInInfo = vehicleRegistrationService.getCarCheckInInfo(action, null, null, startTime, endTime, visitDepartment, page);
        }
        if (getCarCheckInInfo != null) {
            for (CarCheckInInfoDTO carCheckInInfoDTO : getCarCheckInInfo) {
                carCheckInInfoDTO.setIdCard(DesensitizeUtil.hideIdCardNo(carCheckInInfoDTO.getIdCard()));
            }
        }
        return Response.success(page.setRecords(getCarCheckInInfo));
    }

    @ApiParam(value = "获取车辆进出登记信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "action", value = "1-进/2-出"),
            @ApiImplicitParam(name = "nameOrPhone", value = "姓名或手机号"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "visitDepartment", value = "拜访科室")
    })
    @GetMapping("/downCarCheckInInfo")
    public Response downCarCheckInInfo(HttpServletResponse response,
                                       Integer action,
                                       String nameOrPhone,
                                       String startTime,
                                       String endTime,
                                       String visitDepartment) {
        List<CarCheckInInfoDTO> getCarCheckInInfos;
        if (StringUtil.isNotBlank(startTime)) {
            startTime = startTime + " 00:00:00";
        }
        if (StringUtil.isNotBlank(endTime)) {
            endTime = endTime + " 23:59:59";
        }
        if (StringUtil.isNotBlank(nameOrPhone) && StringUtils.isNumeric(nameOrPhone)) {
            getCarCheckInInfos = vehicleRegistrationService.getCarCheckInInfo(action, null, nameOrPhone, startTime, endTime, visitDepartment, null);
        } else if (StringUtil.isNotBlank(nameOrPhone) && !StringUtils.isNumeric(nameOrPhone)) {
            getCarCheckInInfos = vehicleRegistrationService.getCarCheckInInfo(action, nameOrPhone, null, startTime, endTime, visitDepartment, null);
        } else {
            getCarCheckInInfos = vehicleRegistrationService.getCarCheckInInfo(action, null, null, startTime, endTime, visitDepartment, null);
        }

        for (int i = 0; i < getCarCheckInInfos.size(); i++) {
            if (getCarCheckInInfos != null && getCarCheckInInfos.get(i).getAction() != null) {
                if (getCarCheckInInfos.get(i).getAction() == 1) {
                    getCarCheckInInfos.get(i).setActionStr("进");
                } else if (getCarCheckInInfos.get(i).getAction() == 2) {
                    getCarCheckInInfos.get(i).setActionStr("出");
                }
            }
        }
        for (CarCheckInInfoDTO carCheckInInfoDTO : getCarCheckInInfos) {
            carCheckInInfoDTO.setIdCard(DesensitizeUtil.hideIdCardNo(carCheckInInfoDTO.getIdCard()));
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
            String date = sdf.format(new Date());
            String fileName = "车辆登记数据-" + date + ".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
            EasyExcel.write(response.getOutputStream(), CarCheckInInfoDTO.class).sheet("车辆登记数据").doWrite(getCarCheckInInfos);
        } catch (Exception e) {
            log.error("卡口二维码数据导出" + e.getMessage(), e);
        }
        return Response.success();
    }
}
